package com.example.chapter02;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.chapter02.util.DateUtil;
import com.example.chapter02.util.UsUtil;

public class ActResponseActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String mReponse = "我还没睡，我爸妈不在家。";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_response);
        TextView tv_request = findViewById(R.id.tv_request);
        // 从上一个页面传来的意图中获取快递包裹
        Bundle bundle = getIntent().getExtras();
        String request_time = bundle.getString("request_time");
        String request_content = bundle.getString("request_content");
        /*String response_name = bundle.getString("response_name");
        String response_number = bundle.getString("response_number");
        String response_email = bundle.getString("response_email");*/


        TextView name = findViewById(R.id.name);
        TextView number = findViewById(R.id.number);
        TextView email = findViewById(R.id.email);
        //获取本Activity的Intent
        Intent intent = getIntent();
        //获取传递的数据
        UsUtil usUtil = (UsUtil) intent.getSerializableExtra("student");
        name.setText("姓名："+usUtil.getName());
        number.setText("学号："+usUtil.getNumber());
        email.setText("邮箱："+usUtil.getEmail());

        //拿到一个SharedPreference对象
        SharedPreferences sp = getSharedPreferences("usInfo", MODE_PRIVATE);    //config为要生成的文件名
        //拿到编辑器
        SharedPreferences.Editor ed = sp.edit();
        //写数据
        ed.putString("name", name.getText().toString());
        ed.putString("number",number.getText().toString());
        ed.putString("email",email.getText().toString());
        //提交
        ed.commit();
        //显示
        show();

        String desc = String.format("收到请求消息：\n请求时间为%s\n请求内容为%s\n",
                request_time, request_content);
        // 把请求消息的详情显示在文本视图上
        tv_request.setText(desc);

        findViewById(R.id.btn_response).setOnClickListener(this);

        TextView tv_response = findViewById(R.id.tv_response);
        tv_response.setText("待返回的消息为：" + mReponse);
    }

    public void show(){
        //拿到一个SharedPreference对象
        SharedPreferences sp = getSharedPreferences("usInfo", MODE_PRIVATE);
        //从SharedPreference里取数据
        String stuName = sp.getString("name","");
        String stuNumber = sp.getString("number","");
        String stuEmail = sp.getString("email","");
        System.out.println("姓名："+stuName);
        System.out.println("学号："+stuNumber);
        System.out.println("邮箱："+stuEmail);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("response_time", DateUtil.getNowTime());
        bundle.putString("response_content", mReponse);
        intent.putExtras(bundle);
        // 携带意图返回上一个页面。RESULT_OK表示处理成功
        setResult(Activity.RESULT_OK, intent);
        // 结束当前的活动页面
        finish();
    }
}